package cn.ainit.yz.execut;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cn.ainit.sys.common.dao.BusinessBaseDao;
import cn.ainit.sys.utils.UUID32;
import cn.ainit.yz.execut.entity.Execut;
import cn.ainit.yz.execut.entity.ExecutFM;
import cn.ainit.yz.execut.entity.ExecutZD;
import cn.ainit.yz.execut.entity.ExecutZW;
import cn.ainit.yz.printexplain.entity.PrintExplainFM;
import cn.ainit.yz.printexplain.entity.PrintExplainZD;
import cn.ainit.yz.printexplain.entity.PrintExplainZW;
@Transactional
@Repository("executDao")
public class ExecutDaoImpl extends BusinessBaseDao implements ExecutDao {

	@Override
	public boolean saveExBase(Execut execut) throws Exception {
		this.getSessionFactory().getCurrentSession().save(execut);
		return true;
	}

	@Override
	public boolean updateExBase(Execut execut) throws Exception {
		this.getSessionFactory().getCurrentSession().save(execut);
		return true;
	}
	@Override
	public List saveExFM(List<ExecutFM> executFMs, String peId) throws Exception {
		List<String> idsList = new ArrayList<String>();
		if (peId == null || peId.equals("")) {
			throw new RuntimeException("请先保存综合施工单基本信息");
		}
		List<ExecutFM> oldList = new ArrayList<ExecutFM>();
		List<ExecutFM> newList = new ArrayList<ExecutFM>();
		//需要删除的封面印刷ID
		StringBuffer peFMIds = new StringBuffer("");
		//遍历明细 判断相应条件
		for (int i = 0; i < executFMs.size(); i++) {
			if (executFMs.get(i).getExFengmianId() != null && (!executFMs.get(i).getExFengmianId().equals(""))) {
				//说明是原有的封面印刷
				idsList.add(executFMs.get(i).getExFengmianId());
				executFMs.get(i).setExId(peId);
				oldList.add(executFMs.get(i));
				peFMIds.append("'"+executFMs.get(i).getExFengmianId()+"',");
			}else{
				//新添加的封面印刷
				idsList.add(UUID32.newGuid());
				executFMs.get(i).setExId(peId);
				executFMs.get(i).setExFengmianId(idsList.get(i));
				newList.add(executFMs.get(i));
			}
		}
		//删除废弃的封面印刷
		String ids = peFMIds.toString();
		if (!ids.equals("")) {
			ids = ids.substring(0, peFMIds.toString().length()-1);
			String sql = " delete from yz_execut_fm pefm where pefm.ex_id = '"+peId+"' ";
			sql += " and pefm.ex_fengmian_id not in("+ids+") ";
			this.getJdbcTemplate().execute(sql);
		}
		//保存数据
		for (int i = 0; i < oldList.size(); i++) {
			this.getSessionFactory().getCurrentSession().update(oldList.get(i));
		}
		//更新数据
		for (int i = 0; i < newList.size(); i++) {
			this.getSessionFactory().getCurrentSession().save(newList.get(i));
		}
		return idsList;
	}

	@Override
	public List<String> saveExZD(List<ExecutZD> executZDs, String peId)
			throws Exception {
		List<String> idsList = new ArrayList<String>();
		if (peId == null || peId.equals("")) {
			throw new RuntimeException("请先保存综合施工单基本信息");
		}
		List<ExecutZD> oldList = new ArrayList<ExecutZD>();
		List<ExecutZD> newList = new ArrayList<ExecutZD>();
		//需要删除的封面印刷ID
		StringBuffer peFMIds = new StringBuffer("");
		//遍历明细 判断相应条件
		for (int i = 0; i < executZDs.size(); i++) {
			if (executZDs.get(i).getExFengmianId() != null && (!executZDs.get(i).getExFengmianId().equals(""))) {
				//说明是原有的封面印刷
				idsList.add(executZDs.get(i).getExFengmianId());
				executZDs.get(i).setExId(peId);
				oldList.add(executZDs.get(i));
				peFMIds.append("'"+executZDs.get(i).getExFengmianId()+"',");
			}else{
				//新添加的封面印刷
				idsList.add(UUID32.newGuid());
				executZDs.get(i).setExId(peId);
				executZDs.get(i).setExFengmianId(idsList.get(i));
				newList.add(executZDs.get(i));
			}
		}
		//删除废弃的封面印刷
		String ids = peFMIds.toString();
		if (!ids.equals("")) {
			ids = ids.substring(0, peFMIds.toString().length()-1);
			String sql = " delete from yz_execut_zd pefm where pefm.ex_id = '"+peId+"' ";
			sql += " and pefm.ex_fengmian_id not in("+ids+") ";
			this.getJdbcTemplate().execute(sql);
		}
		//保存数据
		for (int i = 0; i < oldList.size(); i++) {
			this.getSessionFactory().getCurrentSession().update(oldList.get(i));
		}
		//更新数据
		for (int i = 0; i < newList.size(); i++) {
			this.getSessionFactory().getCurrentSession().save(newList.get(i));
		}
		return idsList;
	}

	@Override
	public List<String> saveExZW(List<ExecutZW> executZWs, String peId)
			throws Exception {
		List<String> idsList = new ArrayList<String>();
		if (peId == null || peId.equals("")) {
			throw new RuntimeException("请先保存综合施工单基本信息");
		}
		List<ExecutZW> oldList = new ArrayList<ExecutZW>();
		List<ExecutZW> newList = new ArrayList<ExecutZW>();
		//需要删除的封面印刷ID
		StringBuffer peFMIds = new StringBuffer("");
		//遍历明细 判断相应条件
		for (int i = 0; i < executZWs.size(); i++) {
			if (executZWs.get(i).getExFengmianId() != null && (!executZWs.get(i).getExFengmianId().equals(""))) {
				//说明是原有的封面印刷
				idsList.add(executZWs.get(i).getExFengmianId());
				executZWs.get(i).setExId(peId);
				oldList.add(executZWs.get(i));
				peFMIds.append("'"+executZWs.get(i).getExFengmianId()+"',");
			}else{
				//新添加的封面印刷
				idsList.add(UUID32.newGuid());
				executZWs.get(i).setExId(peId);
				executZWs.get(i).setExFengmianId(idsList.get(i));
				newList.add(executZWs.get(i));
			}
		}
		//删除废弃的封面印刷
		String ids = peFMIds.toString();
		if (!ids.equals("")) {
			ids = ids.substring(0, peFMIds.toString().length()-1);
			String sql = " delete from yz_execut_zw pefm where pefm.ex_id = '"+peId+"' ";
			sql += " and pefm.ex_fengmian_id not in("+ids+") ";
			this.getJdbcTemplate().execute(sql);
		}
		//保存数据
		for (int i = 0; i < oldList.size(); i++) {
			this.getSessionFactory().getCurrentSession().update(oldList.get(i));
		}
		//更新数据
		for (int i = 0; i < newList.size(); i++) {
			this.getSessionFactory().getCurrentSession().save(newList.get(i));
		}
		return idsList;
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<ExecutFM> findFMList(String ysId) throws Exception {
		try {
			String hql = "from ExecutFM pr where pr.exId ='"+ysId+"'";
			Query query = this.getSessionFactory().getCurrentSession().createQuery(hql);
			List<ExecutFM> listZds = query.list();
			if (listZds !=null) {
				return listZds;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ExecutZD> findZDList(String ysId) throws Exception {
		try {
			String hql = "from ExecutZD zd where zd.exId = '"+ysId+"'";
			Query query = this.getSessionFactory().getCurrentSession().createQuery(hql);
			List<ExecutZD> listZds = query.list();
			if (listZds !=null) {
				return listZds;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ExecutZW> findZWList(String ysId) throws Exception {
		try {
			String hql = "from ExecutZW zw where zw.exId = '"+ysId+"'";
			Query query = this.getSessionFactory().getCurrentSession().createQuery(hql);
			List<ExecutZW> listZds = query.list();
			if (listZds !=null) {
				return listZds;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public String exeToSprk(String exId,String oId) throws Exception {
		String putValue="";
		Connection conn = null;
		CallableStatement cs =null;
		try {
			 conn= this.getJdbcTemplate().getDataSource().getConnection();
			 String sql = "{Call PK_YZ.up_SgdToRk(?,?,?)}";
			 cs = conn.prepareCall(sql);
			 cs.setString(1,exId);
			 cs.setString(2,oId);
			 cs.registerOutParameter(3,oracle.jdbc.OracleTypes.VARCHAR);
			 cs.execute();
			 putValue = cs.getString(3);
		} catch (Exception e) {
			e.getMessage();
			throw e;
		}finally{
			if(cs != null){
				cs.close();
			}
			if(conn != null){
				conn.close();
			}
		}
		return putValue;
	}
}
